package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.enums.ScoreOperationTypeEnum;
import com.xbongbong.saas.service.ScoreConditionService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 复选框组，下拉框组，标签 的评分判断是在这边
 * dataValue 是数组的在这边处理
 * complexScoreConditionService
 * value属于数组.
 *     {
 * "array_1":[
             256,
             257,
             259,
             255
 * ],
 * "array_2":[
          "317db8c1-21ea-8d6b-68ad-111f1793b72b",//复选框
          "f306a670-9f9a-198e-d86d-9b8980887740"//复选按钮
      ],
  "array_3":[
           "ce0fc934-d0d5-64e5-06b7-6b702a78e5e4",
           "2244243c-7aff-a018-a4c2-c7e1014a4e44"
 * ],
 *     }
 *
 *
 */
@Service("complexScoreConditionService")
public class ComplexScoreConditionServiceImpl implements ScoreConditionService {
    private static final Logger LOG = LoggerFactory.getLogger(SingleScoreConditionServiceImpl.class);
    private static Double DEFAULT_SCORE_VALUE = 0D;

    /**
     * 所有评分数组接口都能兼容
     *
     * @param dataValue
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double empty(Object dataValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            if (Objects.isNull(dataValue) || CollectionUtils.isEmpty((List<Object>) dataValue)) {
                return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
            }
        } catch (Exception e) {
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 所有评分数组接口都能兼容
     *
     * @param dataValue
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double noempty(Object dataValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            if (Objects.nonNull(dataValue) && CollectionUtils.isNotEmpty((List<Object>) dataValue)) {
                return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
            }
        } catch (Exception e) {
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * @param dataValue              支持单值的数据结构，单行文本，多行文本，数字，日期，，单选按钮，下拉框，最后跟进时间，创建时间 （数组另外处理）
     * @param compareValue
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double equal(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            if (equal(dataValue, compareValue)) {
                return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
            }
        } catch (Exception e) {
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * @param dataValue              支持单值的数据结构，单行文本，多行文本，数字，日期，，单选按钮，下拉框，最后跟进时间，创建时间 （数组另外处理）
     * @param compareValue
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double noequal(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            if (!equal(dataValue, compareValue)) {
                return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
            }
        } catch (Exception e) {
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 只有数字 和 时间类型会走该判断，统一处理为Long类型，
     *
     * @param dataValue              支持单值的数据结构，数字，日期，最后跟进时间，创建时间
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double greaterequal(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 只有数字 和 时间类型会走该判断，统一处理为Long类型，
     *
     * @param dataValue              支持单值的数据结构，数字，日期，最后跟进时间，创建时间
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double greatethan(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 只有数字 和 时间类型会走该判断，统一处理为Long类型，
     *
     * @param dataValue              支持单值的数据结构，数字，日期，最后跟进时间，创建时间
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double lessequal(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 只有数字 和 时间类型会走该判断，统一处理为Long类型，
     *
     * @param dataValue              支持单值的数据结构，数字，日期，最后跟进时间，创建时间
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double lessthan(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 只有数字 和 时间类型会走该判断，统一处理为Long类型，
     *
     * @param dataValue              支持单值的数据结构，数字，日期，最后跟进时间，创建时间
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double range(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 包含任意一个
     * @param dataValue       jsonArray 类型      数组才有这种操作，下拉复选框，复选框，标签，此类中不做操作
     * @param compareValue    List 类型            size  必须为 1
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double include(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            //目前 dataValue 值就两种。String 或者 Number. 对于Number 可能会出现，dataValue存的是{25444 integer} ,compareValue{25444 long},这种坑爹的情况。所以都转为 string比较
            if (Objects.nonNull(dataValue) && Objects.nonNull(compareValue)) {
                List<String> dataValueArray = JSONArray.parseArray(JSON.toJSONString(dataValue), String.class);
                List<String> compareValueArray = JSONArray.parseArray(JSON.toJSONString(compareValue), String.class);
                if (CollectionUtils.isNotEmpty(dataValueArray) && CollectionUtils.isNotEmpty(compareValueArray)) {
                    for(Object compareItem : compareValueArray){
                        if(dataValueArray.contains(compareItem)){
                            LOG.info("评分条件：include 命中 dataValue-{}-,compareValue-{}-,scoreOperationValue-{}-,scoreOperationTypeEnum-{}-", JSON.toJSONString(dataValue),JSON.toJSONString(compareValue),JSON.toJSONString(scoreOperationValue),JSON.toJSONString(scoreOperationTypeEnum));
                            return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
                        }
                    }
                }
            }
        }catch (Exception e){
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     * 同时包含
     *
     * @param dataValue
     * @param scoreOperationValue
     * @param scoreOperationTypeEnum
     * @return
     * @throws XbbException
     */
    @Override
    public Double allinclude(Object dataValue, List<Object> compareValue, Double scoreOperationValue, ScoreOperationTypeEnum scoreOperationTypeEnum){
        try {
            if (Objects.nonNull(dataValue) && Objects.nonNull(compareValue)) {
                List<String> dataValueArray = JSONArray.parseArray(JSON.toJSONString(dataValue), String.class);
                List<String> compareValueArray = JSONArray.parseArray(JSON.toJSONString(compareValue), String.class);
                if (CollectionUtils.isNotEmpty(dataValueArray) && CollectionUtils.isNotEmpty(compareValueArray)) {
                    if(dataValueArray.containsAll(compareValueArray)){
                        return ScoreOperationTypeEnum.ADD.equals(scoreOperationTypeEnum) ? scoreOperationValue : -scoreOperationValue;
                    }
                }
            }
        }catch (Exception e){
            return DEFAULT_SCORE_VALUE;
        }
        return DEFAULT_SCORE_VALUE;
    }

    /**
     *条件是等于时， compareValue 的 size是 1，这里的逻辑是判断dataValue是否包含compareValue.get(0)
     * 对于数组的，都比较 size 等于1 的时候
     * dataValue 解析出来， 复选框的是 item  是 String ,  标签的是item存的是 id ,解析出来是Integer,（String）Integer 直接强转是不行的
     * @param dataValue
     * @param compareValue
     * @return
     */
    private Boolean equal(Object dataValue, List<Object> compareValue) {
        if (Objects.nonNull(dataValue) && CollectionUtils.isNotEmpty(compareValue)) {
            List<String> dataValueArray = JSONArray.parseArray(JSON.toJSONString(dataValue), String.class);
            if (CollectionUtils.isNotEmpty(dataValueArray)) {
                Object firstCompareValue = String.valueOf(compareValue.get(0));
                return dataValueArray.contains(firstCompareValue);
            }
        }
        return false;
    }


}
